package com.eco
{
	import com.eco.*;
	import com.framework.*;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.*;
		
	//道具类，继承了基本物体，还拥有改变
	public class Bonus extends RigidBall
	{
		protected var mcTexture:MovieClip;
		protected var ftFighter:Fighter;//卫士类的引用，用于获取卫士类的一些参数
		protected var vecEnemy:Vector.<Enemy>;//敌人数组引用，用于获取敌人类的参数
		
		public var iType:int;//道具的类型
		protected var iExistTime:int;//道具存在场上的时间，超过时间没有被吃道具会消失
		protected var iFunctionTime:int;//道具作用持续时间，延迟为0则为瞬发技能
		protected var tClock:Timer;//用于计时的Timer
		
		//以下申明各种类型的道具
		static public const BONUS_SPEEDUP:int = 0;//加速
		static public const BONUS_SUPERARMOR:int = 1;//霸体，不会硬直
		static public const BONUS_BLAST:int = 2;//以卫士为中心发出冲击波震击所有敌人
		static public const BONUS_MASSUP:int = 3;//质量增大，重装更猛
		static public const BONUS_EXPLOSIVE:int = 4;//撞击敌人后，敌人直接爆炸
		static public const BONUS_INVINCIBLE:int = 5;//狂暴无敌，兼顾0123项功能
		
		public var iState:int;//道具的状态
		
		//以下声明各种状态
		static public const STATE_IDLE:int = 0;
		static public const STATE_ACTIVE:int = 1;
		static public const STATE_END:int = 2;
		
		public function Bonus( r:int = 10, type:int = 0 )
		{	
			//基类构造函数，建立刚体
			super( 10,r);
			
			//初始化敌人类型
			iType = type;
			CreateEnemyMovieClip(type);
			
			//这两个时间不要设置得完全一样，时间相同时请留有个位数的差别
			iExistTime = 10001;
			iFunctionTime = 6000;
			
			tClock = new Timer(iExistTime,1);
			tClock.start();
			tClock.addEventListener(TimerEvent.TIMER, StateChange );
			
			
			ftFighter = null;
			vecEnemy = null;
			
			iState = STATE_IDLE;
			CreateSoundEvent( CustomEventSound.PLAY_SOUND, SoundInfo.EFF_BONUS_APPEAR);
			
		}
		
		private function CreateEnemyMovieClip(type:int):void
		{
			switch (type)
			{
				case BONUS_SPEEDUP:
				{
					mcTexture = new resBonusSpeedUp();
					break;
				}
				case BONUS_SUPERARMOR:
				{
					mcTexture = new resBonusSuperArmor();
					break;
				}
				case BONUS_BLAST:
				{
					mcTexture = new resBonusBlast();
					break;
				}
				case BONUS_MASSUP:
				{
					mcTexture = new resBonusMassUp();
					break;
				}
				case BONUS_EXPLOSIVE:
				{
					mcTexture = new resBonusExplosive();
					break;
				}
				case BONUS_INVINCIBLE:
				{
					mcTexture = new resBonusInvincible();
					break;
				}
			}
			mcTexture.scaleX = 0.8;
			mcTexture.scaleY = 0.8;
			mcTexture.gotoAndPlay("IDLE");
			this.visible = false;
			addChild(mcTexture);
		}
		
		private function StateChange(e:TimerEvent):void
		{	
			if( iState == STATE_IDLE )
			{
				//闲置状态下时间到，表示存在时间已经达到上限，需要移除
				iState = STATE_END;
			}
			else if( iState == STATE_ACTIVE )
			{
				//激活状态时间到，表示使用持续时间已经结束，停止道具功能后移除
				StopFunction();
				iState = STATE_END;
			}
		}
		
		public function Update():void
		{
			//道具更新的几个部分
			Move();//执行移动模块
			Rotate();//执行自转模块
			
			//状态管理
			switch( iState )
			{
				case STATE_IDLE:
				{
					break;
				}
				case STATE_ACTIVE:
				{
					if( tClock.delay != iFunctionTime )
					{
						//初次进入STATE_ACTIVE状态时更新周期
						tClock.delay = iFunctionTime;
					}
					bConflicting = false;//一旦被出发就不能再被碰撞检测，防止重复吃道具
					break;
				}
				case STATE_END:
				{
					//停止始终，并且移除侦听器
					tClock.stop();
					tClock.removeEventListener(TimerEvent.TIMER, StateChange );
					break;
				}
			}
		}
		
		public function SetFighter(fighter:Fighter):void
		{
			ftFighter = fighter;
		}
		
		public function SetEnemyVector(vecenemy:Vector.<Enemy>):void
		{
			vecEnemy = vecenemy;
		}
		
		protected function CreateSoundEvent(type:String, name:String ):void
		{
			var evt:CustomEventSound = new CustomEventSound( type, name );
			dispatchEvent(evt);
		}
		
		public function StartFunction():void
		{
			this.bConflicting = false;
			this.x = ftFighter.x;
			this.y = ftFighter.y;
			mcTexture.gotoAndPlay("ACTIVE");
			iState = STATE_ACTIVE;
		}
		
		public function StopFunction():void
		{
			
		}
		
		public function Pause():void
		{
			tClock.stop();
			mcTexture.stop();
		}
		
		public function Continue():void
		{
			tClock.start();
			mcTexture.play();
		}
		

	}
}